/*###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############*/

document.captureEvents = false;

var AdminMenu = {
    ajaxUrl : '',
    pages : '',
    blockOrder : '',
    saveTemplate : '',
    menuMapper : Array(),
    urlMapper : Array(),
    menuHoverId : '',
    blockMapper : Array(),
    hashRegistry : Array(),
    
    init : function(pageid){
        this.ajaxUrl = AjaxUrl;

        $('#jfx-pages').click(function(){
            AdminMenu.pages = this;

            $('#jfx-pages-menu').show('slow');
        }, function(){})
        
        this.menuMapper['jfx-toolbar'] = $('#jfx-toolbar').get(0);
        this.menuMapper['jfx-pages'] = $('#jfx-pages-menu').get(0);

        this.urlMapper['jfx-toolbar'] = 'toolbar/';
        this.urlMapper['jfx-pages'] = 'pages-menu/';

        $('#jfx-block-order').click(function(){
            var href = AjaxUrl+'/block-execution-order/?page='+AdminBlocks.pageid+'&KeepThis=true&TB_iframe=true&height=500&width=700';
            tb_show('Editing Block Execution Order', href, false);
        });

        

    },

    getValue : function(id, alt){
        for(var i=0; i<this.hashRegistry.length; i++){
            if(this.hashRegistry[i].id == id) return this.hashRegistry[i].val;
        }
        return alt;
    },

    getKey : function(id){
        for(var i=0; i<this.hashRegistry.length; i++){
            if(this.hashRegistry[i].id == id) return i;
        }
        var key = this.hashRegistry.length;
        this.hashRegistry[key] = new Object();
        return key;
    },

    setValue : function(id, val){
        var key = this.getKey(id);
        this.hashRegistry[key].id = id;
        this.hashRegistry[key].val = val;
    },


    getMenuContent : function(itemId){
        if(typeof(this.urlMapper[itemId])!=='string') return;
        this.menuHoverId = itemId;
        $.get(this.ajaxUrl+'/'+this.urlMapper[itemId]+'?page='+AdminBlocks.pageid, this.showMenuContent);
    },

    showMenuContent : function(text){
        AdminMenu.parseContent(AdminMenu.menuHoverId, text);
    },

    parseContent : function(menuId, text){
        var menuContent = this.menuMapper[menuId];
        switch(menuId){
            case 'jfx-toolbar':
                
                var json = JSON.parse(text);

                AdminMenu.menuMapper[AdminMenu.menuHoverId].innerHTML = json.content;
                // got to make the items draggable
               $('#jfx-toolbar').draggable();
                
                eval(json.functions);
                
                JFXToolbarHide();

            break;
            case 'jfx-pages':
                // first example
                AdminMenu.menuMapper[AdminMenu.menuHoverId].innerHTML = text;
                $("#jfx-page-tree").treeview({
                    persist: "location",
                    collapsed: true,
                    unique: true
                });
                $('#jfx-pages-menu-inner a.edit-page').unbind('click');
                $('#jfx-pages-menu-inner a.edit-page').click(function(){
                    var href = AjaxUrl+'/edit-page/?page='+AdminBlocks.pageid+'&KeepThis=true&TB_iframe=true&height=500&width=700';
                    tb_show('Editing Page Details', href, false);
                });

            break;
        }
    },

    confirmSavePage : function(success, message){
        if(success){
            alert('Save successful!');
            //window.location = this.saveLocation;
        }else{
            alert('Error saving: '+message);
        }
    },

    getBlockDetails : function(blockId){
        $.getJSON(this.ajaxUrl+'/get-block-details/', {blockId : blockId, pageid : AdminBlocks.page.id}, this.storeBlockDetails);
    },

    storeBlockDetails : function(json){
        AdminMenu.setValue('blockMapper_'+json.id, json);
    }
}



